package com.example.demo.config;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.setting.dialect.Props;
import lombok.Data;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Map;


/**
 * 读取项目相关配置
 *
 * @author administrator
 */
@Data
public class SystemConfig {

    private SystemConfig() {
    }

    private static SystemConfig config;
    private static Props PROPS;
    private static String path = "config/system.properties";
    private static String absolutePath;

    static {
        // 查找 resources/config/system.properties
        File file = new File(ResourceUtil.getResourceObj(path).getUrl().getPath());
        if (!file.exists()) {
            // 查找 config/system.properties
            file = new File(path);
        }
        absolutePath = file.getAbsolutePath();
        if (!file.exists()) {
            new Props(path, StandardCharsets.UTF_8).store(path);
        }
        PROPS = new Props(file, StandardCharsets.UTF_8);
        PROPS.autoLoad(true);
    }

    public static void main(String[] args) {
        //SystemConfig systemConfig = new SystemConfig();
        //System.out.println(systemConfig);
        SystemConfig instance = SystemConfig.instance();
        System.out.println(instance);
        // System.exit(0);
    }

    /**
     * 系统名称
     */
    private String title;
    /**
     * 系统版本
     */
    private String version;
    /**
     * 系统描述
     */
    private String desc;
    /**
     * 出品公司
     */
    private String produce;
    /**
     * 系统logo
     */
    private String logo;
    /**
     * 版权年份
     */
    private String copyrightYear;
    /**
     * 上传文件路径
     */
    private String uploadPath;
    /**
     * websocket 地址
     */
    private String websocket;
    /**
     * 密码后缀
     */
    private String defaultPassword;
    /**
     * 验证码开关
     */
    private Boolean verifyCode;
    /**
     * 同一账号同时登录最大用户数
     */
    private Integer maxSessions;
    /**
     * 登录信息有效期
     */
    private Long expTime;
    /**
     * 默认登陆用户类型
     */
    private Integer defType;

    /**
     * 是否框架
     */
    private Boolean isFramework;

    private Dict ext;

    public void put(String key, Object value) {
        if (ext == null) {
            ext = Dict.create();
        }
        ext.put(key, value);
    }

    public <T> T get(String key, T def) {
        if (ext == null) {
            ext = Dict.create();
        }
        return ext.get(key, def);
    }

    public static SystemConfig instance() {
        // 用这种形式形成单例
        if (config == null) {
            config = PROPS.toBean(SystemConfig.class);
        }
        return config;
    }

    private void setProperty(Map<String, Object> config, String prefix) {
        config.forEach((key, val) -> {
            if (val instanceof Dict) {
                setProperty((Dict) val, key + ".");
            } else {
                PROPS.setProperty(prefix + key, ObjectUtil.defaultIfNull(val, ""));
            }
        });
    }

    public void store(Map<String, Object> config) {
        setProperty(config, "");
        PROPS.store(absolutePath);
    }

    public void store() {
        store(BeanUtil.beanToMap(this, false, true));
    }
}

